package com.core.web.struts2.taglib;

import java.util.*;
import org.apache.struts2.views.jsp.*;

import javax.servlet.jsp.*;

import com.core.util.*;

/**
 * date JSP 标签
 * 
 * 
 */
public class DateTag extends CommonTag {

	private String cachename = "";

	@Override
	public int doStartTag() throws JspException {
		return SKIP_BODY;
	}

	@Override
	public int doEndTag() throws javax.servlet.jsp.JspException {
		try {
			pageContext.getOut().print(getHtml());
		} catch (Exception e) {
			throw new JspException(e);
		}
		return EVAL_BODY_INCLUDE;
	}

	private String getHtml() {
		StringBuffer ret = new StringBuffer();
		Object stackObject = TagUtils.getStack(this.pageContext)
				.findValue(name);
		String inputValue = "";

		if (getValue() != null && !getValue().equals("")) {
			inputValue = getValue();
		}
		if (stackObject != null && !stackObject.equals("")
				&& (stackObject instanceof Date)) {
			inputValue = DateUtil.formatLongDate(((Date) stackObject));
		}

		if (cachename != null) {
			if (cachename.equals("now_long")
					&& (inputValue == null || inputValue.equals(""))) {
				inputValue = DateUtil.formatLongNow();
			} else if (cachename.equals("now_short")
					&& (inputValue == null || inputValue.equals(""))) {
				inputValue = DateUtil.formatShortNow();
			}
		}

		if (this.format != null && !this.format.equals("")
				&& inputValue != null && !inputValue.equals("")) {
			try {
				if (format.equals("date_short") && inputValue.length() >= 10) {
					inputValue = inputValue.substring(0, 10);
				} else if (format.equals("date_long")) {
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		ret.append("<input type=\"text\" ").append(super.getCommonHtml())
				.append(" value=\"").append(inputValue).append("\" >");
		return ret.toString();
	}

	/**
	 * @return the cachename
	 */
	public String getCachename() {
		return cachename;
	}

	/**
	 * @param cachename
	 *            the cachename to set
	 */
	public void setCachename(String cachename) {
		this.cachename = cachename;
	}
}
